package com.etang.a2020c1020.fragment;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.etang.a2020c1020.R;

import java.util.ArrayList;
import java.util.List;

public class BarActivity extends FragmentActivity {

    ViewPager pager;
    PagerCreate pagerCreate;
    List<Fragment> list_fragment = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_bar);
        list_fragment.add(new BaseFragment());
        list_fragment.add(new ControlFragment());
        list_fragment.add(new VlueFragment());
        pager = (ViewPager) findViewById(R.id.pager);
        pagerCreate = new PagerCreate(getSupportFragmentManager());
        pager.setAdapter(pagerCreate);
        pager.setOffscreenPageLimit(list_fragment.size());
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.e("TAG", "onPageScrolled: " + position);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public class PagerCreate extends FragmentPagerAdapter {

        public PagerCreate(@NonNull FragmentManager fm) {
            super(fm);
            Log.e("TAG", "onPageScrolled: 创建完成");
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            Log.e("TAG", "onPageScrolled: 创建完成");
            return list_fragment.get(position);
        }

        @Override
        public int getCount() {
            Log.e("TAG", "onPageScrolled: 创建完成");
            return list_fragment.size();
        }
    }

}
